package code.jam.core;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import my.andoird.util.r1.JamThread;
import android.util.Log;
import code.jam.context.JamBGMContext;
import code.jam.context.JamConfigContext;
import code.jam.context.JamMIFContext;
import code.jam.context.JamURI;
import code.jam.interfaces.JamBGMCompletionListener;

public class JamBGMManager extends java.lang.Object
{

	public static final String				DEBUG_TAG			= "JamBGMManager";

	private HashMap<String, JamBGMContext>	map;
	private JamBGMContext					current_bgm_context;
	private List<String>					bgm_titles;
	private List<String>					bgm_file_names;
	private Thread thread = null;
	private JamBGMCompletionListener bgm_completion_listener = null;
	
	private boolean is_loading = false;
	private boolean is_checked = false;

	public JamBGMManager()
	{
		super();
		
		map = new HashMap<String, JamBGMContext>();
		current_bgm_context = null;
		bgm_file_names = null;
		bgm_titles = null;
	}

	public boolean start(JamConfigContext context)
	{
		boolean is_success = true;
		
		is_loading = false;
		is_checked = checkDownload(context);

		update(context);
		addAll(context);
		
		is_loading = true;
		is_checked = true;

		Log.d(DEBUG_TAG, "jam bgm manager is done...");

		return is_success;

	}
	
	public boolean isChecked()
	{
		return is_checked;
	}
	
	public boolean checkDownload(JamConfigContext context)
	{
		Vector<String> bgm_vector_string = context.getBgm();
		Vector<String> mif_vector_string = context.getMIF();
		
		File[] files = new File(JamURI.JAM_SD_BGM_PATH).listFiles();
		
		if ((files == null) || (files.length == 0) || (files.length != bgm_vector_string.size()))
		{
			return false;
		}
		
		files = new File(JamURI.JAM_SD_MIF_PATH).listFiles();

		if ((files == null) || (files.length == 0) || (files.length != mif_vector_string.size()))
		{
			return false;
		}
		
		return true;
	}
	
	public void addBGMCompletionListener(JamBGMCompletionListener listener)
	{
		bgm_completion_listener = listener;

		thread = new Thread( new Runnable()
		{
			public void run()
			{
				while( true )
				{
					boolean b = ((current_bgm_context.isStopped()) || (current_bgm_context.isPlaying() == false)); 

					bgm_completion_listener.onBGMPlayingCompletion( b );

					if( b == true )
					{
						Log.d(DEBUG_TAG, "drop THREAD_BGM_COMPLETION_LISTENER");
						break;
					}

					JamThread.sleep(500);
				}
			}
		}, "THREAD_BGM_COMPLETION_LISTENER");

		thread.start();
	}

	private void update(JamConfigContext context)
	{
		Vector<String> bgm_vector_string = context.getBgm();
		Vector<String> mif_vector_string = context.getMIF();

		JamDataManager.downloadBGM(bgm_vector_string);
		JamDataManager.downloadMIF(mif_vector_string);

		return;
	}

	private void addAll(JamConfigContext context)
	{
		Vector<String> bgms = context.getBgm();
		Vector<String> mifs = context.getMIF();

		for (String bgm_name : bgms)
		{
			String title = bgm_name.substring(0, bgm_name.length() - 4);

			int mif_index = mifs.indexOf(title + ".mif");

			if (mif_index != -1)
			{
				String mif_name = mifs.elementAt(mif_index);

				JamBGMContext jbc = new JamBGMContext(title, bgm_name, mif_name);

				add(jbc);
			}
		}
	}

	public void add(JamBGMContext context)
	{
		map.put(context.getTitle(), context);

		return;
	}

	public JamBGMContext get(String name)
	{
		JamBGMContext context = this.map.get(name);

		return context;
	}

	public Collection<JamBGMContext> get()
	{
		Collection<JamBGMContext> collection = this.map.values();

		return collection;
	}

	public List<String> getBGMFileNames()
	{
		this.bgm_file_names = new ArrayList<String>(getBGMSize());

		Set<String> key = this.map.keySet();

		Iterator<String> iter = key.iterator();

		while (iter.hasNext())
		{
			String name = iter.next();

			this.bgm_file_names.add(name);
		}

		return this.bgm_file_names;
	}

	public List<String> getTitles()
	{
		this.bgm_titles = new ArrayList<String>(getBGMSize());

		Set<String> key = this.map.keySet();

		Iterator<String> iter = key.iterator();

		while (iter.hasNext())
		{
			JamMIFContext mif = this.map.get(iter.next()).getMIFContext();

			if (mif != null)
			{
				this.bgm_titles.add(mif.getTitle());
			}
		}

		return this.bgm_titles;
	}

	public void remove(String name)
	{
		map.remove(name);
	}

	public void drop()
	{
		if (this.map != null)
		{
			Collection<JamBGMContext> coll = this.map.values();

			Iterator<JamBGMContext> iter = coll.iterator();

			while (iter.hasNext())
			{
				JamBGMContext context = iter.next();
				context.drop();
				Log.d(DEBUG_TAG, String.format("music(%s) is drop...", context.getTitle()));
			}
		}

		return;
	}

	public void setCurrentBgmContext(String title)
	{
		if (this.map != null)
		{
			Collection<JamBGMContext> coll = this.map.values();

			Iterator<JamBGMContext> iter = coll.iterator();

			JamBGMContext context = null;

			while (iter.hasNext())
			{
				context = iter.next();

				if (context.getMIFContext() != null)
				{
					if (context.getMIFContext().getTitle() == title)
					{
						this.current_bgm_context = context;
						break;
					}
				}
			}

			Log.d(DEBUG_TAG, "current BGM title is : " + title);
		}
		else
		{
			Log.e(DEBUG_TAG, " can't setup current BGM title :  " + title);
		}
	}

	public JamBGMContext getSelectedBGMContext()
	{
		return current_bgm_context;
	}

	private int getBGMSize()
	{
		return map.size();
	}

	public boolean isLoaded() 
	{
		return is_loading;
	}
}
